package com.yf.zzmusic.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.common.Result;
import com.yf.zzmusic.pojo.dto.collect.CollectDeleteDTO;
import com.yf.zzmusic.pojo.dto.collect.CollectPageDTO;
import com.yf.zzmusic.pojo.vo.collect.CollectTableVO;
import com.yf.zzmusic.pojo.vo.music.MusicTableVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.yf.zzmusic.service.ICollectService;
import com.yf.zzmusic.pojo.entity.Collect;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 我喜欢，注意需要有逻辑删除，用来恢复 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-07
 */
@RestController
@RequestMapping("/collect")
public class CollectController {
  @Resource
  private ICollectService collectService;

  @PostMapping("/insert")
  public Result save(@Validated @RequestBody Collect collect) {
    boolean result = collectService.save(collect);
    if (result) {
      return Result.success("新增成功", null);
    } else {
      return Result.error("新增失败");
    }
  }

  /** 物理删除，注意：物理删除必须通过表的唯一id进行删除 */
  @DeleteMapping("/{id}")
  public Result delete(@PathVariable Integer id) {
    boolean result = collectService.removeById(id);
    if (result) {
      return Result.success("删除成功", null);
    } else {
      return Result.error("删除失败");
    }
  }

  /**
   * 逻辑删除
   */
  @PostMapping("/delete")
  public Result logicDelete(@Validated @RequestBody CollectDeleteDTO collectDeleteDTO) {
    boolean result = collectService.logicDeleteCollectById(collectDeleteDTO);
    if (result) {
      return Result.success("删除成功", null);
    } else {
      return Result.error("删除失败");
    }
  }

  /** 逻辑恢复 */
  @PostMapping("/recovery")
  public Result logicRecovery(@Validated @RequestBody CollectDeleteDTO collectDeleteDTO) {
    boolean result = collectService.logicRecoveryCollectById(collectDeleteDTO);
    if (result) {
      return Result.success("恢复成功", null);
    } else {
      return Result.error("恢复失败");
    }
  }

  @GetMapping("/page")
  public Result findPage(@Validated CollectPageDTO collectPageDTO) {
    PageInfo<CollectTableVO> pageInfo = collectService.collectDetailsByPage(collectPageDTO);
    if (pageInfo.getData().size() > 0) {
      return Result.success("查询成功", pageInfo);
    } else {
      return Result.error("查询失败");
    }
  }
}

